﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParsingProject
{
    public sealed class ParserHelper
    {
        private Dictionary<string, StreamWriter> _writers;
        private Dictionary<string, StreamReader> _readers;
        private static ParserHelper __instance;

        private static ParserHelper _instance
        {
            get
            {
                if (__instance == null)
                    __instance = new ParserHelper();

                return __instance;
            }
            set
            {
                __instance = value;
            }
        }
        
        private ParserHelper() 
        {
            _readers = new Dictionary<string, StreamReader>();
            _writers = new Dictionary<string, StreamWriter>();
        }

        public static bool ReadFile(string path)
        {
            FileStream fs = null;
            StreamReader sr = null;

            try
            {
                fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);

                _instance._readers[path] = sr;
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e.Message);

                if (sr != null)
                {
                    sr.Close();
                }

                return false;
            }

            return true;
        }

        public static bool CreateFile(string path)
        {
            FileStream fs = null;
            StreamWriter sw = null;

            try
            {
                fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                sw = new StreamWriter(fs);
                _instance._writers[path] = sw;
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e.Message);

                if (sw != null)
                {
                    sw.Close();
                }

                return false;
            }

            return true;
        }

        public static void Write(string path, string text)
        {
            StreamWriter sw = _instance._writers[path];

            sw.Write(text);
        }

        public static void WriteLine(string path, string text)
        {
            StreamWriter sw = _instance._writers[path];

            sw.WriteLine(text);
        }

        public static void WriteLine(string path)
        {
            StreamWriter sw = _instance._writers[path];

            sw.WriteLine();
        }

        public static string ReadLine(string path)
        {
            StreamReader sr = _instance._readers[path];

            return sr.ReadLine();
        }

        public static string[] ReadAndParseLine(string path, params char[] delims)
        {
            StreamReader sr = _instance._readers[path];

            if (delims.Count() != 0)
                return sr.ReadLine().Split(delims);
            return sr.ReadLine().Split(new char[] { ' ' });
        }

        public static string[] ReadAndParseLine(string path, int count, params char []delims)
        {
            StreamReader sr = _instance._readers[path];

            if (delims.Count() != 0)
                return sr.ReadLine().Split(delims, count);
            return sr.ReadLine().Split(new char[] { ' ' }, count);
        }

        public static bool EndOfStream(string path)
        {
            StreamReader sr = _instance._readers[path];

            return sr.EndOfStream;
        }

        public static void CloseReadFile(string path)
        {
            StreamReader sr = _instance._readers[path];

            if (sr != null)
            {
                sr.Close();
            }
        }

        public static void CloseWriteFile(string path)
        {
            StreamWriter sw = _instance._writers[path];

            if (sw != null)
            {
                sw.Close();
            }
        }

    }
}
